<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');

class LotteryController extends AdminbaseController{
	public function index(){
		$request = I("request.");
		$where = array();
		$p = I("p");
		if($request['name']){
            $where['lc.name']=array('like',"%".$request['name']."%");
            $this->assign("name",$request['name']);
        }
        if($request['type']){
            $where['lc.lottery_type_id']=$request['type'];
            $this->assign("typeid",$request['type']);
        }
		$count = M("lottery_class lc")
					->join("join ".C('DB_PREFIX')."lottery_type lt on lc.lottery_type_id=lt.id")
					->where($where)
					->count();
		$page = $this->page($count,10);
		$data = M("lottery_class lc")
					->join("join ".C('DB_PREFIX')."lottery_type lt on lc.lottery_type_id=lt.id")
					->where($where)
					->field("lc.*,lt.name typename")
					->limit($page->firstRow, $page->listRows)
					->select();
		$type = M("lottery_type")->select();
		if(!empty($data) && is_array($data)){
    		$existdata=true;
    	}else{
    		$existdata=false;
    	}
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("existdata", $existdata);
    	$this->assign("data", $data);
    	$this->assign("type", $type);
    	$this->assign("p", $p);
		$this->display();
	}
	/*
	 * 是否开售
	 */
	public function lottery_calss_type(){
		$search = array(
			'id' =>I("id"),
			'type' =>I("type")
		);
		M("lottery_class")->where("id = {$search['id']}")->save(array('type'=>$search['type']));
		$this->success();
	}
	public function xg_time(){
		$time = I("get.time");
		$type = I("get.type");
		$id = I("get.id");
		$result = M("lottery_class")->where("id='$id'")->save(array("$type"=>$time));
		if($result){
			echo "1";
		}else{
			echo "0";
		}
	}
	//人工开奖
	public function rengong(){
		$id = I("id");
		$p = I("p");
		$res = M("lottery_class")->where("id = $id")->find();
		$a = C("LOTTERY")[$id];
		$qis = array();
		if($id=='1'){
			$di = date("Ymd")."082";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='2'){
			$di = date("Ymd")."080";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='3'){
			$di = date("Ymd")."080";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='4'){
			$di = date("Ymd")."078";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='5'){
			//北京
			$ttt = date("Y-m-d");
			$st = M("day_start")->where("date = '{$ttt}' and type = 1")->find();
			$start = $res['issue']+1;
			if($start < $st['issue']+89){
				for($i = $res['issue']+1;$i<=$st['issue']+89;$i++){
					$qis[] = '0'.$i;
				}
			}
		}else if($id=='9'){
			$di = date("Ymd")."081";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='10'){
			$di = date("Ymd")."072";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='11'){
			$di = date("Ymd")."082";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='12'){
			$di = date("Ymd")."078";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='13'){
			$di = date("Ymd")."079";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='15'){
			$di = "120";
			$start = $res['issue']+1;
			if(substr($start,-3) < $di){
				$end = substr($start,0,8).$di;
				for($i = $res['issue']+1;$i<=$end;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='17'){
			$di = "96";
			$start = $res['issue']+1;
			if(substr($start,-2) < $di){
				$end = substr($start,0,9).$di;
				for($i = $res['issue']+1;$i<=$end;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='18'){
			$di = date("Ymd")."084";
			$start = $res['issue']+1;
			if($start3 < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='19'){
			//埃及
		}else if($id=='20'){
			//快乐8
			$ttt = date("Y-m-d");
			$st = M("day_start")->where("date = '{$ttt}' and type = 2")->find();
			$start = $res['issue']+1;
			if($start < $st['issue']+179){
				for($i = $res['issue']+1;$i<=$st['issue']+179;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='21'){
			//pk10
			$ttt = date("Y-m-d");
			$st = M("day_start")->where("date = '{$ttt}' and type = 3")->find();
			$start = $res['issue']+1;
			if($start < $st['issue']+179){
				for($i = $res['issue']+1;$i<=$st['issue']+179;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='22'){
			$di = date("Ymd")."84";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='23'){
			$di = date("Ymd")."090";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='24'){
			$di = date("Ymd")."087";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='25'){
			$di = date("Ymd")."084";
			$start = $res['issue']+1;
			if($start < $di){
				for($i = $res['issue']+1;$i<=$di;$i++){
					$qis[] = $i;
				}
			}
		}else if($id=='26'){
			//福彩3d-
			$qis[] = $res['issue']+1;
		}else{
			//pl3
			$qis[] = $res['issue']+1;
		}
		$this->assign("res",$res);
		$this->assign("qis",$qis);
		$this->assign("p",$p);
		$this->display();
	}
	public function is_qis(){
		$search = array(
			'lotteryclass_id' => $_POST['id'],
			'issue' => $_POST['qihao']
		);
		$res = M("ren_gong")->where("lotteryclass_id = '{$search['lotteryclass_id']}' and issue = '{$search['issue']}'")->find();
		$code = C('CLASS_CODE');
		$zoushi = M("zoushitu")->where("issue='{$search['issue']}' and code='{$code[$search['lotteryclass_id']]}'")->find();
		if($res||!empty($zoushi['opencode'])){
			echo 1;die;
		}else{
			echo 0;die;
		}
	}
	public function adds_post(){
		$p = I("p");
		$search = array(
			'lotteryclass_id' => $_POST['lottery_class_id'],
			'issue' => $_POST['qihao'],
			'opencode' => $_POST['conts'],
		);
		$q = M("lottery_class")->where("id = '{$search['lotteryclass_id']}'")->find();
		$cha = $search['issue'] - $q['issue'];
		$search['opentimestamp'] = $q['opentimestamp']+10*60*$cha;
		$search['opentime'] = date("Y-m-d H:i:s",$q['opentimestamp']+10*60*$cha);
        $code = C('CLASS_CODE');
        $zoushi = M("zoushitu")->where("issue='{$search['issue']}' and code='{$code[$search['lotteryclass_id']]}'")->find();
        if(empty($zoushi)){
            $date = array(
                'issue' => $search['issue'],
                'opencode' => $search['opencode'],
                'opentime' =>$search['opentime'],
                'code' => $code[$search['lotteryclass_id']],
            );
            M("zoushitu")->add($date);
        }else{
            M("zoushitu")->where("id = {$zoushi['id']}")->save(array('opencode'=> $search['opencode']));
        }
		M("ren_gong")->add($search);
		$this->success("添加成功",U("Lottery/index",array("p"=>$p)));
	}
}
?>